<template>
  <div class="resources-page">
    <div class="page-header">
      <h1>资源下载</h1>
      <div class="search-box">
        <input 
          v-model="searchQuery" 
          type="text" 
          placeholder="搜索资源..."
        >
        <i class="icon-search"></i>
      </div>
    </div>

    <div class="filters">
      <div class="filter-group">
        <label>资源类型：</label>
        <select v-model="selectedType">
          <option value="">全部</option>
          <option v-for="type in resourceTypes" :key="type" :value="type">
            {{ type }}
          </option>
        </select>
      </div>

      <div class="filter-group">
        <label>排序方式：</label>
        <select v-model="sortBy">
          <option value="latest">最新上传</option>
          <option value="popular">下载最多</option>
          <option value="rating">评分最高</option>
        </select>
      </div>
    </div>

    <div class="resources-grid">
      <div v-for="resource in filteredResources" :key="resource.id" class="resource-card card">
        <div class="resource-header">
          <h3>{{ resource.title }}</h3>
          <span class="resource-type">{{ resource.type }}</span>
        </div>
        
        <p class="resource-description">{{ resource.description }}</p>
        
        <div class="resource-meta">
          <div class="meta-item">
            <i class="icon-download"></i>
            <span>{{ resource.downloads }} 次下载</span>
          </div>
          <div class="meta-item">
            <i class="icon-star"></i>
            <span>{{ resource.rating }}/5.0</span>
          </div>
          <div class="meta-item">
            <i class="icon-time"></i>
            <span>{{ resource.date }}</span>
          </div>
        </div>

        <div class="resource-footer">
          <div class="resource-tags">
            <span v-for="tag in resource.tags" :key="tag" class="tag">
              {{ tag }}
            </span>
          </div>
          <button class="btn btn-primary download-btn">
            下载资源
          </button>
        </div>
      </div>
    </div>

    <div class="pagination">
      <button 
        class="btn btn-secondary" 
        :disabled="currentPage === 1"
        @click="currentPage--"
      >
        上一页
      </button>
      <span class="page-info">第 {{ currentPage }} 页，共 {{ totalPages }} 页</span>
      <button 
        class="btn btn-secondary" 
        :disabled="currentPage === totalPages"
        @click="currentPage++"
      >
        下一页
      </button>
    </div>
  </div>
</template>

<script setup>
const searchQuery = ref('')
const selectedType = ref('')
const sortBy = ref('latest')
const currentPage = ref(1)
const totalPages = ref(5)

const resourceTypes = [
  '电子书',
  '视频教程',
  '工具软件',
  '设计素材',
  '代码模板',
  '其他资源'
]

const resources = [
  {
    id: 1,
    title: 'Vue.js 实战教程',
    type: '视频教程',
    description: '从入门到精通，包含大量实战案例和最佳实践。',
    downloads: 1234,
    rating: 4.8,
    date: '2024-03-15',
    tags: ['Vue.js', '前端', '视频教程']
  },
  {
    id: 2,
    title: 'React 组件库',
    type: '代码模板',
    description: '包含常用UI组件和业务组件的React组件库。',
    downloads: 856,
    rating: 4.5,
    date: '2024-03-10',
    tags: ['React', '组件库', 'UI']
  },
  {
    id: 3,
    title: 'Node.js 性能优化指南',
    type: '电子书',
    description: '深入讲解Node.js性能优化技巧和最佳实践。',
    downloads: 567,
    rating: 4.7,
    date: '2024-03-05',
    tags: ['Node.js', '性能优化', '后端']
  },
  {
    id: 4,
    title: 'UI设计素材包',
    type: '设计素材',
    description: '包含图标、插画、背景等设计资源。',
    downloads: 2345,
    rating: 4.9,
    date: '2024-03-01',
    tags: ['设计', 'UI', '素材']
  }
]

const filteredResources = computed(() => {
  let result = [...resources]
  
  // 搜索过滤
  if (searchQuery.value) {
    const query = searchQuery.value.toLowerCase()
    result = result.filter(resource => 
      resource.title.toLowerCase().includes(query) ||
      resource.description.toLowerCase().includes(query) ||
      resource.tags.some(tag => tag.toLowerCase().includes(query))
    )
  }
  
  // 类型过滤
  if (selectedType.value) {
    result = result.filter(resource => resource.type === selectedType.value)
  }
  
  // 排序
  switch (sortBy.value) {
    case 'latest':
      result.sort((a, b) => new Date(b.date) - new Date(a.date))
      break
    case 'popular':
      result.sort((a, b) => b.downloads - a.downloads)
      break
    case 'rating':
      result.sort((a, b) => b.rating - a.rating)
      break
  }
  
  return result
})
</script>

<style scoped>
.resources-page {
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.search-box {
  position: relative;
  width: 300px;
}

.search-box input {
  width: 100%;
  padding: 12px 16px;
  padding-right: 40px;
  border: 1px solid var(--border-color);
  border-radius: 4px;
}

.search-box i {
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-secondary);
}

.filters {
  display: flex;
  gap: 24px;
  margin-bottom: 24px;
}

.filter-group {
  display: flex;
  align-items: center;
  gap: 12px;
}

.filter-group select {
  padding: 8px 12px;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  background-color: white;
}

.resources-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 24px;
  margin-bottom: 24px;
}

.resource-card {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.resource-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.resource-header h3 {
  margin: 0;
  font-size: 18px;
}

.resource-type {
  padding: 4px 8px;
  background-color: var(--bg-secondary);
  border-radius: 4px;
  font-size: 12px;
}

.resource-description {
  color: var(--text-secondary);
  margin: 0;
}

.resource-meta {
  display: flex;
  gap: 16px;
  color: var(--text-secondary);
  font-size: 14px;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 4px;
}

.resource-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: auto;
}

.resource-tags {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.tag {
  padding: 4px 8px;
  background-color: var(--bg-secondary);
  border-radius: 4px;
  font-size: 12px;
}

.download-btn {
  white-space: nowrap;
}

.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 16px;
  margin-top: 32px;
}

.page-info {
  color: var(--text-secondary);
}

@media (max-width: 768px) {
  .page-header {
    flex-direction: column;
    gap: 16px;
  }

  .search-box {
    width: 100%;
  }

  .filters {
    flex-direction: column;
    gap: 16px;
  }

  .filter-group {
    flex-direction: column;
    align-items: flex-start;
  }

  .resources-grid {
    grid-template-columns: 1fr;
  }
}
</style> 